package tool.log;

import java.io.Serializable;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

public class Log4JLogger implements Log, Serializable {

    private static final long serialVersionUID = 2290363532618951965L;

    private static final String FQCN = Log4JLogger.class.getName();

    private transient Logger logger;

    private String name;

    private static Priority traceLevel;

    static {
        if (!Priority.class.isAssignableFrom(Level.class)) {
            throw new InstantiationError("Log4J 1.2 not available");
        }

        try {
            traceLevel = (Priority) Level.class.getDeclaredField("TRACE").get(null);
        } catch (Exception ex) {
            traceLevel = Level.DEBUG;
        }
    }

    public Log4JLogger() {

    }

    public Log4JLogger(String name) {

        this.name = name;
        this.logger = getLogger();
    }

    public Log4JLogger(Logger logger) {

        this.name = logger.getName();
        this.logger = logger;
    }

    public void trace(Object message) {

        getLogger().log(FQCN, traceLevel, message, null);
    }

    public void trace(Object message, Throwable t) {

        getLogger().log(FQCN, traceLevel, message, t);
    }

    public void debug(Object message) {

        getLogger().log(FQCN, Level.DEBUG, message, null);
    }

    public void debug(Object message, Throwable t) {

        getLogger().log(FQCN, Level.DEBUG, message, t);
    }

    public void info(Object message) {

        getLogger().log(FQCN, Level.INFO, message, null);
    }

    public void info(Object message, Throwable t) {

        getLogger().log(FQCN, Level.INFO, message, t);
    }

    public void warn(Object message) {

        getLogger().log(FQCN, Level.WARN, message, null);
    }

    public void warn(Object message, Throwable t) {

        getLogger().log(FQCN, Level.WARN, message, t);
    }

    public void error(Object message) {

        getLogger().log(FQCN, Level.ERROR, message, null);
    }

    public void error(Object message, Throwable t) {

        getLogger().log(FQCN, Level.ERROR, message, t);
    }

    public void fatal(Object message) {

        getLogger().log(FQCN, Level.FATAL, message, null);
    }

    public void fatal(Object message, Throwable t) {

        getLogger().log(FQCN, Level.FATAL, message, t);
    }

    public Logger getLogger() {

        if (logger == null) {
            logger = Logger.getLogger(name);
        }
        return (this.logger);
    }

    public boolean isDebugEnabled() {

        return getLogger().isDebugEnabled();
    }

    public boolean isErrorEnabled() {

        return getLogger().isEnabledFor(Level.ERROR);
    }

    public boolean isFatalEnabled() {

        return getLogger().isEnabledFor(Level.FATAL);
    }

    public boolean isInfoEnabled() {

        return getLogger().isInfoEnabled();
    }

    public boolean isTraceEnabled() {

        return getLogger().isEnabledFor(traceLevel);
    }

    public boolean isWarnEnabled() {

        return getLogger().isEnabledFor(Level.WARN);
    }
}
